/*  This file is part of the comicconquer project
    Copyright (C) 2010 Reach <reach334@gmail.com>

    ComicConquer is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This software is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public 
    License along with this software.  If not, see <http://www.gnu.org/licenses/>.

*/

#ifndef PROCESSTHREAD_H
#define PROCESSTHREAD_H

#include "mist.h"
#include "PageStruct.h"
#include "Configuration.h"
#include "ZipContainer.h"

#include <QtCore/QThread>
#include <QtGui/QtGui>

/**
 * The thread where the core image processing run.
 * 
 * @author reach (5/26/2010)
 */
class ProcessThread : public QThread
{
    Q_OBJECT

    //-- constructor and destructor
public:
    ProcessThread(QObject* parent = 0);
    ~ProcessThread();

    //-- public methods
public:
    bool LoadDir(QDir& dir, QStringList& fileNames, ImageProfile& imgProfile, int iPage, int iDivPage);
    bool LoadZip(ZipContainer* zipContainer, ImageProfile& imgProfile, int iPage, int iDivPage);
    DivPage* RequestPage(int iPage, int iDivPage = 0, bool isPrev = false);
    bool updateProfile(ImageProfile& imgProfile, int iPage = 0, int iDivPage = 0);
    void reOpenZip();

    //-- signals
    signals:
    void SendPage(DivPage* page);

    //-- private methods
private:
    void run();
    void loadPage(Page& page);
    void processPage(DivPage& divPage);
    void imageAnalyze(DivPage& divPage);
    void analyzeContour(CvSeq* contour, LineList& hLines, LineList& vLines, int& linedis,
                        IplImage*& cvimgThresh, IplImage*& cvimgContour, IplImage*& cvimgLine);
    bool degreeCorrect(DivPage& divPage, LineList& hLines, LineList& vLines,
                       int linedis, int& width, int& height);
    void cutMargin(DivPage& divPage, CvSeq* contour,
                   LineList& hLines, LineList& vLines,
                   int& width, int& height, float& ratio);
    void findScrollLines(DivPage& divPage, LineList& hLines, LineList& vLines, int& width, int& height);
    bool isGridCrossVLine(GridForScroll& grid, Line& vLine); 
    void combineScrollLines(LineList& vLine, GridForScrollVec& grids);
    int connectType(Line& hLine, Line& vLine);

    //-- private members
private:
    QMutex m_mutex;

    QWaitCondition m_condition;

    QDir m_dir;
    QStringList m_sFileNames;
    ZipContainer* m_zipContainer;
    bool m_restart;
    bool m_abort;
    bool m_request;
    bool m_isPrev;
    int m_iPage;
    int m_iPageCount;
    int m_iDivPage;
    int m_iDivCount;

    int m_processW;

    ImageProfile m_imgProfile;
    PageQueue m_pageQueue;
    QMutex m_queueMutex;
};

#endif  // PROCESSTHREAD_H
